package com.zysc.processflow.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtils {


    public static long getDay(int add){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar = Calendar.getInstance();
        Date date = new Date(GetCurrentMillisecond());
        calendar.setTime(date);
        calendar.add(Calendar.DATE,add);
        try {
            return sdf.parse(new SimpleDateFormat("yyyyMMdd000000").format(calendar.getTime())).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static long getFirstDayOfMonth(int add){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.MONTH,add);
        try {
            return sdf.parse(new SimpleDateFormat("yyyyMMdd000101").format(calendar.getTime())).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static long getLastDayOfMonth(int add){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.MONTH,add);
        try {
            return sdf.parse(new SimpleDateFormat("yyyyMMdd235959").format(calendar.getTime())).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static long getFirstTimeOfOneDay(int add){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,add);
        try {
            return sdf.parse(new SimpleDateFormat("yyyyMMdd000101").format(calendar.getTime())).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static long getLastTimeOfOneDay(int add){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,add);
        try {
            return sdf.parse(new SimpleDateFormat("yyyyMMdd235959").format(calendar.getTime())).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static Long GetCurrentMillisecond(){
        //格式化时间戳
        LocalDateTime ldt = LocalDateTime.now();
        Long tm = ldt.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return tm;
    }
    /**
     * 获取精确到秒的时间戳
     * @param date
     * @return
     */
    public static Long getSecondTimestamp(LocalDateTime date){
        if (null == date) {
            return 0L;
        }
        Long tm = date.toInstant(ZoneOffset.of("+8")).toEpochMilli()/1000;
        return tm;
    }
    /**
     * 获取精确到毫秒的时间戳
     * @param date
     * @return
     */
    public static Long getMilliTimestamp(LocalDateTime date){
        if (null == date) {
            return 0L;
        }
        Long tm = date.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return tm;
    }

    public static String FormatDate(Date date, String formater){
        if(formater == null || formater == "") {
            formater="yyyy-MM-dd HH:mm:ss";
        }
        //格式化时间
        SimpleDateFormat createDateFormat = new SimpleDateFormat(formater);
        return createDateFormat.format(date);
    }
    /**
     * 获取当前日期前一天日期
     * @param
     * @return
     */
    public static String getYesterdayDate(){
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天
        Date dBefore = calendar.getTime();
        SimpleDateFormat createDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday=createDateFormat.format(dBefore);
        return yesterday;
    }

    public static long getTodayStart(){
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, -12);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTimeInMillis();
    }

    public static long getTodayEnd(){
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 11);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTimeInMillis();
    }
}
